package com.qf.servlet;

import com.alibaba.fastjson.serializer.CollectionCodec;
import com.qf.entity.Card1;
import com.qf.entity.Card2;
import com.qf.entity.User;
import com.qf.service.Card1Service;
import com.qf.service.Card2Service;
import com.qf.service.UserService;
import com.qf.service.impl.Card1ServiceImpl;
import com.qf.service.impl.Card2ServiceImpl;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.JdbcUtils;


import javax.naming.Name;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
import java.net.IDN;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.CollationElementIterator;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/card")
public class CardServlet extends BaseServlet {
    private Card1Service card1Service = new Card1ServiceImpl();
    private Card2Service card2Service = new Card2ServiceImpl();
    private UserService userService = new UserServiceImpl();

    private void openAccount2(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        Integer uid = (Integer) req.getSession().getAttribute("id");
        String password = req.getParameter("password");
        String password1 = req.getParameter("password1");
        String name = req.getParameter("name");
        String idNum = req.getParameter("idNum");
        String result = "";
        String phone = req.getParameter("phone");

        if (password.length() != 6 || password1.length() != 6) {
            result = "falsePwdLength";
        } else if (idNum.length() != 18) {
            result = "falseID";
        } else if (!password.equals(password1)) {
            result = "falsePwd";
        }else {
            Card2 card2 = new Card2(null, name, idNum, phone, password, null, uid,0.0);
            boolean add = card2Service.add(card2);
            result = "true";
        }
        writeHTML(result, resp);
    }

    private void openAccount1(HttpServletRequest req, HttpServletResponse resp) throws IOException, InvocationTargetException, IllegalAccessException {
        Integer uid = (Integer) req.getSession().getAttribute("id");
        String bankType = req.getParameter("bankType");
        String password = req.getParameter("password");
        String password1 = req.getParameter("password1");
        String name = req.getParameter("name");
        String idNum = req.getParameter("idNum");
        String result = "";
        String phone = req.getParameter("phone");

        if (password.length() != 6 || password1.length() != 6) {
            result = "falsePwdLength";
        } else if (!password.equals(password1)) {
            result = "falsePwd";
        }else if (phone.length() != 11){
            result = "falsePhone";
        }else {
            Card1 card1 = new Card1(null, name, phone, password, bankType, uid,0.0);
            boolean add = card1Service.add(card1);
            result = "true";
        }
        writeHTML(result, resp);
    }

    private void findAllCard2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer uid = (Integer) req.getSession().getAttribute("id");
        List<Card2> allCard2 = card2Service.findAllCard2(uid);
        writeHTML(allCard2, resp);
    }

    private void findAllCard1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer uid = (Integer) req.getSession().getAttribute("id");
        List<Card1> allCard1 = card1Service.findAllCard1(uid);
        writeHTML(allCard1, resp);
    }

    private void binding(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Integer uid = (Integer) req.getSession().getAttribute("id");
        String name = req.getParameter("name");
        String id1 = req.getParameter("id1");
        String id2 = req.getParameter("id2");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        Card1 card = new Card1(id1,name,phone,password,uid);
        Card1 card1 = card1Service.findOneCard1(card);
        boolean bingding = card2Service.bingding(id1,id2);
        String result = "";
        if (card1 == null){
            result = "falseNull";
        }else {
            if (bingding){
                result = "true";
            }else {
                result = "falseNull";
            }
        }
        writeHTML(result,resp);
    }
    private void tixian(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String id2 = req.getParameter("id2");
        String name = req.getParameter("name");
        String id1 = req.getParameter("id1");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        String money1 = req.getParameter("money");
        double money = Double.parseDouble(money1);
        //返回前端的结果
        String jieguo = "";
        Connection connection = JdbcUtils.getConnection();
        //根据前端传过来的数据判断是否存在这个卡号
        boolean result = card1Service.yanzheng(id1,name,phone,password);
        if (result){
            //找到二类卡
            Card2 card2 = card2Service.findCard2ById(id2);
            Double balance = card2.getBalance();
            //判断是否余额不足
            if (balance >= money){
                try {
                    //设置手动提交事务
                    connection.setAutoCommit(false);
                    boolean result1 = card2Service.tixian(id2,money);
                    boolean result2 = card1Service.huodetixian(id1,money);
                    if (result1&&result2){//如果都成功
                        connection.commit();
                        System.out.println("提现成功");
                    }else {
                        throw new SQLException("提现失败");
                    }
                    jieguo = "true";
                } catch (Exception e) {
                    connection.rollback();
                    JdbcUtils.closeAll(connection,null,null);
                }finally {
                    JdbcUtils.closeAll(connection,null,null);
                }
            }else {
                jieguo = "lowBalance";
            }
        }else {
            jieguo="false";
        }
        writeHTML(jieguo,resp);

    }

    private void chongzhi(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String id2 = req.getParameter("id2");
        String name = req.getParameter("name");
        String id1 = req.getParameter("id1");
        String phone = req.getParameter("phone");
        String password = req.getParameter("password");
        String money1 = req.getParameter("money");
        double money = Double.parseDouble(money1);
        //返回前端的结果
        String jieguo = "";
        Connection connection = JdbcUtils.getConnection();
        //根据前端传过来的数据判断是否存在这个卡号
        boolean result = card1Service.yanzheng(id1,name,phone,password);
        if (result){
            //找到一类卡
            Card1 card1 = card1Service.findByID(id1);
            Double balance = card1.getBalance();
            //判断是否余额不足
            if (balance >= money){
                try {
                    //设置手动提交事务
                    connection.setAutoCommit(false);
                    boolean result2 = card2Service.huodechongzhi(id2,money);
                    boolean result1 = card1Service.chongzhi(id1,money);
                    if (result1&&result2){//如果都成功
                        connection.commit();
                    }else {
                        jieguo = "false";
                    }
                    jieguo = "true";
                } catch (Exception e) {
                    connection.rollback();
                    JdbcUtils.closeAll(connection,null,null);
                }finally {
                    JdbcUtils.closeAll(connection,null,null);
                }
            }else {
                jieguo = "lowBalance";
            }
        }else {
            jieguo="false";
        }
        writeHTML(jieguo,resp);

    }

    private void zhuanzhang(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String id2 = req.getParameter("id2");
        String type = req.getParameter("type");
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        String password = req.getParameter("password");
        String money1 = req.getParameter("money");
        if (money1.equals("")){
            writeHTML("balanceNull",resp);
        }
        double money = Double.parseDouble(money1);
        //返回前端的结果
        String jieguo = "";
        Connection connection = JdbcUtils.getConnection();
        //根据前端传过来的数据判断是否存在这个卡号
        if (type.equals("一类卡")){
            Card1 card1 = card1Service.yanzheng(id, name);
            Card2 card2 = card2Service.findCard2ById(id2);
            if (card1 != null){
                Double balance = card2.getBalance();
                //判断是否余额不足
                if (balance >= money && password.equals(card2.getPassword())){
                    try {
                        //设置手动提交事务
                        connection.setAutoCommit(false);
                        boolean result2 = card2Service.tixian(id2,money);
                        boolean result1 = card1Service.huodetixian(id,money);
                        if (result1&&result2){//如果都成功
                            connection.commit();
                            jieguo = "true";
                        }else {
                            jieguo = "false";
                        }
                    } catch (Exception e) {
                        connection.rollback();
                        JdbcUtils.closeAll(connection,null,null);
                    }finally {
                        JdbcUtils.closeAll(connection,null,null);
                    }
                }else if (!password.equals(card2.getPassword())){
                    jieguo = "falsePwd";
                }else {
                    jieguo = "lowBalance";
                }
            }else {
                jieguo="false";
            }
        }else{
            Card2 card2 = card2Service.yanzheng(id, name);
            Card2 card2_2 = card2Service.findCard2ById(id2);
            if (card2 != null){
                Double balance = card2_2.getBalance();
                //判断是否余额不足
                if (balance >= money && password.equals(card2_2.getPassword())){
                    try {
                        //设置手动提交事务
                        connection.setAutoCommit(false);
                        boolean result2 = card2Service.huodechongzhi(id2,money);
                        boolean result1 = card2Service.tixian(id,money);
                        if (result1&&result2){//如果都成功
                            connection.commit();
                            jieguo = "true";
                        }else {
                            jieguo = "false";
                        }
                    } catch (Exception e) {
                        connection.rollback();
                        JdbcUtils.closeAll(connection,null,null);
                    }finally {
                        JdbcUtils.closeAll(connection,null,null);
                    }
                }else if (!password.equals(card2_2.getPassword())){
                    jieguo = "falsePwd";
                }else {
                    jieguo = "lowBalance";
                }
            }else {
                jieguo="false";
            }
        }

        writeHTML(jieguo,resp);

    }

    private void zhuanzhang1(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        String id2 = req.getParameter("id2");
        String type = req.getParameter("type");
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        String password = req.getParameter("password");
        String money1 = req.getParameter("money");
        if (money1.equals("")){
            writeHTML("balanceNull",resp);
        }
        double money = Double.parseDouble(money1);
        //返回前端的结果
        String jieguo = "";
        Connection connection = JdbcUtils.getConnection();
        //根据前端传过来的数据判断是否存在这个卡号
        if (type.equals("一类卡")){
            //验证是否存在要转账的一类卡
            Card1 card1 = card1Service.yanzheng(id, name);
            //找到支付的一类卡
            Card1 card1pay = card1Service.findByID(id2);
            if (card1 != null){
                Double balance = card1pay.getBalance();
                //判断是否余额不足
                if (balance >= money && password.equals(card1pay.getPassword())){
                    try {
                        //设置手动提交事务
                        connection.setAutoCommit(false);
                        boolean result1pay = card1Service.chongzhi(id2,money);
                        boolean result1 = card1Service.huodetixian(id,money);
                        if (result1&&result1pay){//如果都成功
                            connection.commit();
                            jieguo = "true";
                        }else {
                            jieguo = "false";
                        }
                    } catch (Exception e) {
                        connection.rollback();
                        JdbcUtils.closeAll(connection,null,null);
                    }finally {
                        JdbcUtils.closeAll(connection,null,null);
                    }
                }else if (!password.equals(card1pay.getPassword())){
                    jieguo = "falsePwd";
                }else {
                    jieguo = "lowBalance";
                }
            }else {
                jieguo="false";
            }
        }else{
            Card2 card2 = card2Service.yanzheng(id, name);
            Card1 card1 = card1Service.findByID(id2);
            if (card2 != null){
                Double balance = card1.getBalance();
                //判断是否余额不足
                if (balance >= money && password.equals(card1.getPassword())){
                    try {
                        //设置手动提交事务
                        connection.setAutoCommit(false);
                        boolean result1 = card1Service.chongzhi(id2,money);
                        boolean result2 = card2Service.huodechongzhi(id,money);
                        if (result1&&result2){//如果都成功
                            connection.commit();
                            jieguo = "true";
                        }else {
                            jieguo = "false";
                        }
                    } catch (Exception e) {
                        connection.rollback();
                        JdbcUtils.closeAll(connection,null,null);
                    }finally {
                        JdbcUtils.closeAll(connection,null,null);
                    }
                }else if (!password.equals(card1.getPassword())){
                    jieguo = "falsePwd";
                }else {
                    jieguo = "lowBalance";
                }
            }else {
                jieguo="false";
            }
        }

        writeHTML(jieguo,resp);
    }
}

